#include "view_heart_rate.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "arm_math.h"
#include "app_trace.h"
#include "lfs_file_manager.h"
#include "view_heart_rate.inc"
#include "stopwatch.h"
#include "view_common.h"
#include "language_text.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_heart_rate_animation(view_heart_rate_param const *param)
{
    if (param == 0 || param->file_index_start < DISP_LFS_FILE_INDEX_BEGIN || param->file_index_start >= DISP_LFS_FILE_INDEX_END)
    {
        return;
    }

    if (!param->is_wear)
    {
        int x = 25;
        int y = 164;
        if (param->unwear_language && param->unwear_language_str)
        {
            y -= 1;
            disp_show_font_string_utf8_mid(y, param->unwear_language_str, 0);
        }
        else
        {
            _disp_show_flash_img(param->file_index_start, x, y, FILE_IMG_J120_watch_25_164);
        }
        x = 41;
        y = 49;
        _disp_show_flash_img(param->file_index_start + 1, x, y, FILE_IMG_J120_watch_41_49);
        return;
    }
    int x = 98;
    int y = 34;
    int custom_ui_index = 0;
#ifdef CUSTOM_UI
    int const temp_index_start = param->temp_index_start;

    for (int i = 0; i < HEART_RATE_BACK_COUNT; i++)
    {
        disp_show_resource_with_x_y(param->file_index_start + custom_ui_index, localize_resource_key_heart_rate_back_start + i, temp_index_start + custom_ui_index, -1, -1);
        ++custom_ui_index;
    }
#if HEART_RATE_GIF_COUNT > 0
    disp_show_resource_with_x_y(param->file_index_start + custom_ui_index, localize_resource_key_heart_rate_gif_start + disp_ctrl.auto_refresh_count % HEART_RATE_GIF_COUNT,
                                temp_index_start + custom_ui_index, -1, -1);
    ++custom_ui_index;
#endif

#else
    img_gif_com_t const img_cfg = {
        .gif_count = 3,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_B253_heartrate_icon_1_98_34,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_B254_heartrate_icon_2_98_34,
        .img[2].x = x,
        .img[2].y = y,
        .img[2].sn = FILE_IMG_B255_heartrate_icon_3_98_34,
    };
    _disp_show_flash_img_gif_all(&img_cfg, param->file_index_start + custom_ui_index);
    ++custom_ui_index;
#endif
    int unit_width = 0;
    if (param->unit_width > 0)
    {
        unit_width = param->unit_width;
    }
    int x_center = 120 - unit_width / 2;
    int const font_width = 19;
    int const num_width = font_width + 4;
    int const num_height = 42;
    int unit_height = num_height;
    if (param->unit_height > 0)
    {
        unit_height = param->unit_height;
    }
    y = 111;
    if (param->num < 0)
    {
        x = x_center - num_width;
        if (disp_ctrl.auto_refresh_count % 2 == 0)
        {
            int x1 = x;
            int y1 = y + num_height - unit_height / 2;
            int x2 = x1 + font_width;
            int y2 = y1 + 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);

            x1 += num_width;
            x2 = x1 + font_width;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
        else
        {
        }
        x += num_width * 2;
    }
    else
    {
        if (param->num_digit > 0)
        {
            x = disp_draw_user_image_num_mid_ignore_with_digit(x_center, y, RGB565_COLOR_BLACK, param->num, num_width, param->num_digit, num_N400_dig_19x42_TAB);
        }
        else
        {
            x = disp_draw_user_image_num_mid_ignore(x_center, y, RGB565_COLOR_BLACK, param->num, num_width, num_N400_dig_19x42_TAB);
        }
    }
    y += num_height - param->unit_height;
    _disp_show_flash_img(param->file_index_start + custom_ui_index, x, y, param->unit_index);
    ++custom_ui_index;
}
int32_t view_heart_rate_display(int32_t view)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
    }
    int num = disp_ctrl.com_data.i_temp[0];
    if (num == 0)
    {
        num = -1;
    }
    view_heart_rate_param const param = {
        .is_wear = disp_ctrl.com_data.i_temp[1],
        .num = num,
        .num_digit = 0,
        .unit_index = FILE_IMG_B213_heartrate_bpm, // height 134
        .file_index_start = DISP_LFS_FILE_INDEX_1,
        .unit_height = 17,
        .unit_width = 50,
        .temp_index_start = 50,
        .unwear_language = disp_ctrl.com_data.uint8_temp[150],
        .unwear_language_str = (char const*)&disp_ctrl.com_data.uint8_temp[151],

    };
    view_heart_rate_animation(&param);

    if (disp_ctrl.com_data.i_temp[1])
    {
        int x = 81;
        int y = 189;
#ifdef CUSTOM_UI
        if (disp_ctrl.com_data.uint8_temp[100])
        {
            y -= 5;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_B212_heartrate_text);
        }
        //  bool ret = disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_heart_rate_text, 40);
        // if (!ret)
        // {
        //     _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_B212_heartrate_text);
        // }
#else
        if (disp_ctrl.com_data.uint8_temp[100])
        {
            y -= 5;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_B212_heartrate_text);
        }

        x = 213;
        y = 91;
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I201_btn_up);
        y = 137;
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I202_btn_down);
#endif
    }
    return 0;
}
view_event_result_t view_heart_rate_event(int32_t view, event_param_t event)
{
    Expects(view >= view_heart_rate);
    Expects(view < view_heart_rate + VIEW_LEVEL_0_SUB_COUNT);

    switch (event.event)
    {
#if 0
#warning debug
#else
    case event_move_level_down:
        return view_event_result_handled;
#endif
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
        // stopwatch_start();
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        // LOG("view heart_rate entry");
        {
            uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
            uint32_t buff_len = &disp_ctrl.com_data.uint8_temp[150] - buff;
            disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Heart_rate, buff, buff_len);

            buff = &disp_ctrl.com_data.uint8_temp[151];
            buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
            disp_ctrl.com_data.uint8_temp[150] = get_language_text(language_text_index_Not_wearing_a_watch, buff, buff_len);
        }
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        // stopwatch_stop();
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        // LOG("view heart_rate exit");
        return view_event_result_handled;
    }
#if 0
    return view_event_result_handled;
#warning debug
#else
    return view_event_result_not_handled;
#endif
}
